package com.gcb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.gcb.common.enums.CreateTypeEnum;
import com.gcb.common.enums.OrderStateEnum;
import com.gcb.common.enums.ShipTypeEnum;
import com.gcb.common.enums.TransportType;
import com.gcb.common.utils.OrderUtils;
import com.gcb.domain.dto.OrderShipQueryDTO;
import com.gcb.domain.dto.OrderShipSaveDTO;
import com.gcb.domain.entity.OrderShip;
import com.gcb.domain.vo.OrderShipVO;
import com.gcb.mapper.OrderShipMapper;
import com.gcb.service.IOrderShipService;
import com.gcb.service.IPortService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class OrderShipServiceImpl implements IOrderShipService {

    @Resource
    private OrderShipMapper orderShipMapper;
    @Resource
    private IPortService portService;


    @Override
    public void add(OrderShipSaveDTO orderShipSaveDto, Long currentUserId) {
        OrderShip orderShip = new OrderShip();
        BeanUtil.copyProperties(orderShipSaveDto, orderShip);
        //订单号自动生成
        orderShip.setOrderNo(OrderUtils.generateOrderNo("S"));

        // 设置订单状态 当前没有审核流程，直接审核通过，状态为进行中
        orderShip.setState(OrderStateEnum.UNDERWAY.getCode());
        orderShip.setCreateBy(currentUserId.toString());
        orderShip.setCreateTime(new Date());
        orderShip.setUpdateBy(currentUserId.toString());
        orderShip.setUpdateTime(new Date());
        orderShipMapper.insert(orderShip);
    }

    @Override
    public void update(OrderShipSaveDTO orderShipSaveDto, Long currentUserId) {
        OrderShip orderShip = new OrderShip();
        BeanUtil.copyProperties(orderShipSaveDto, orderShip);
        orderShip.setUpdateBy(currentUserId.toString());
        orderShip.setUpdateTime(new Date());
        orderShipMapper.update(orderShip);
    }


    @Override
    public void delete(int id) {
        orderShipMapper.delete(id);
    }

    @Override
    public OrderShipVO getDetail(Long id) {
        OrderShipVO orderShipVO = orderShipMapper.getDetail(id);
        getEnumInfo(orderShipVO);
        return orderShipVO;
    }

    @Override
    public List<OrderShipVO> list(OrderShipQueryDTO queryDTO) {
        // 根据城市搜索，先根据城市获取港口列表，这样可以少关联数据库表
        if (StrUtil.isNotBlank(queryDTO.getCityName())) {
            List<String> portList = portService.selectNameByCity(queryDTO.getCityName());
            if (portList != null && !portList.isEmpty()) {
                queryDTO.setPortList(portList);
            }
        }
        // 搜索日期
        String[] originDate = queryDTO.getOriginDate();
        if (Objects.nonNull(originDate) && originDate.length == 2) {
            queryDTO.setOriginDateStart(originDate[0]);
            queryDTO.setOriginDateEnd(originDate[1]);
        }

        List<OrderShipVO> orderShipVOList = orderShipMapper.list(queryDTO);
        for (OrderShipVO orderShipVO : orderShipVOList) {
            getEnumInfo(orderShipVO);
        }
        return orderShipVOList;
    }

    public void getEnumInfo(OrderShipVO orderShipVO) {
        //订单状态
        if (orderShipVO.getState() != null) {
            orderShipVO.setStateStr(OrderStateEnum.getInfo(orderShipVO.getState()));
        }
        //船舶类型
        if (orderShipVO.getShipType() != null) {
            orderShipVO.setShipTypeStr(ShipTypeEnum.getInfoByCode(orderShipVO.getShipType()));
        }
        //运输类型
        if (orderShipVO.getTransportType() != null) {
            orderShipVO.setTransportTypeStr(TransportType.getInfoByCode(orderShipVO.getTransportType()));
        }
        // 创建类型
        if (orderShipVO.getCreateType() != null) {
            orderShipVO.setCreateTypeStr(CreateTypeEnum.getInfo(orderShipVO.getCreateType()));
        }
    }

    @Override
    public void batchUpdateState(Long[] ids, Integer state) {
        // TODO 鉴权要调整，这个地方先设置为空
        // Long updateBy = SecurityUtils.getLoginUser().getUserId();
        Long updateBy = 0L;
        orderShipMapper.batchUpdateState(ids, state, updateBy);
    }
}
